<?php


namespace app\api\controller;


use app\common\model\Love;
use app\common\model\Tags;
use think\facade\Filesystem;

class Member
{

    public $memberData;
    public $memberModel;

    public function __construct()
    {
        $token = post_param('token');
        if (empty($token)) output_error('非法访问~');
        $this->memberModel = new \app\common\model\Member();
        $this->memberData=$this->memberModel->where('member_token', '=', $token)->find();
        if (empty($this->memberData)) output_error('令牌无效~');
    }

    public function getData()
    {
        output_data($this->memberData);
    }

    public function addTags()
    {
        $title = post_param('title');
        $link = post_param('link');
        if (empty($title) || empty($link)) output_error_param();
        $tagsModel = new Tags();
        $condition = array();
        $condition[] = ['member_id', '=', $this->memberData['member_id']];
        $condition[] = ['tags_title', '=', $title];
        $condition[] = ['tags_link', '=', $link];
        $data = $tagsModel->where($condition)->find();
        if (!empty($data)) output_error('此书签已存在');
        $data = array();
        $data['member_id'] = $this->memberData['member_id'];
        $data['tags_title'] = $title;
        $data['tags_link'] = $link;
        $data['add_time'] = time();
        $tagsModel->insert($data);
        output_success();
    }

    public function getTags()
    {
        $keyword = post_param('keyword');
        $condition = array();
        $condition[] = ['member_id', '=', $this->memberData['member_id']];
        if (!empty($keyword))$condition[] = ['tags_title','like','%' . $keyword . '%'];
        $tagsModel = new Tags();
        $data = $tagsModel->where($condition)->select();
        output_data($data);
    }

    public function delete()
    {
        $id = post_param('id');
        if (empty($id)) output_error_param();
        $tagsModel = new Tags();
        $tagsModel->where('tags_id','=',$id)->delete();
        output_success();
    }

    public function clear(){
        $tagsModel = new Tags();
        $tagsModel->where('member_id','=',$this->memberData['member_id'])->delete();
        output_success();
    }

    public function addLove()
    {
        $text = post_param('text');
        $file = request()->file('file');
        $savename = Filesystem::disk('public')->putFile('', $file, 'uniqid');
        if (empty($text) || empty($savename)) output_error_param();
        $loveModel = new Love();
        $data = array();
        $data['member_id'] = $this->memberData['member_id'];
        $data['love_avatar'] = $savename;
        $data['text'] = $text;
        $data['add_time'] = time();
        $loveModel->insert($data);
        output_success();
    }

    public function getLove()
    {
        $keyword = post_param('keyword');
        $condition = array();
        $condition[] = ['member_id', '=', $this->memberData['member_id']];
        if (!empty($keyword))$condition[] = ['text','like','%' . $keyword . '%'];
        $loveModel = new Love();
        $data = $loveModel->where($condition)->select();
        foreach($data as $value) $value['avatar_url'] = '127.0.0.1/upload/' . $value['love_avatar'];
        output_data($data);
    }

    public function deletelove()
    {
        $id = post_param('id');
        if (empty($id)) output_error_param();
        $loveModel = new Tags();
        $loveModel->where('love_id','=',$id)->delete();
        output_success();
    }

    public function clearlove(){
        $loveModel = new Love();
        $loveModel->where('member_id','=',$this->memberData['member_id'])->delete();
        output_success();
    }

    public function upload()
    {
        //接受前端传过来的文件
        $file = request()->file('file');
        $savename = Filesystem::disk('public')->putFile('', $file, 'uniqid');
        $update = array();
        $update['member_avatar'] = $savename;
        $this->memberModel->where('member_id', '=', $this->memberData['member_id'])->update($update);
        output_success();
    }
}